#include "main.h"

// Define button inputs
DigitalIn button1(PA_14);
DigitalIn button2(PH_1);
DigitalIn button3(PC_1);
DigitalIn button4(PD_2);

// Global variables
uint16_t imu_lock = 0; // Lock timer for tilt-based navigation (to prevent rapid flipping)
uint8_t key_slow = 0;  // Key scan rate limiter
uint8_t key_value = 0; // Current key value (each bit represents a key)
uint8_t key_up = 0;    // Key release detection
uint8_t key_old = 0;   // Previous key state

// Function to read key and handle input actions
void key_read() {
    if (key_slow) return; // Limit key scan rate
    key_slow = 1;

    key_value = key_scan(); // Read current key state
    key_up = ~key_value & (key_old ^ key_value); // Detect key release
    key_old = key_value; // Update old key state

    // Handle menu and screen state transitions
    if (screen_state == 0) // Main menu
    {
        if (key_up & 0x01 || ((imudata.gy > 80.00) && (imu_lock == 0))) 
        {  
            // Move selection up (button1 or tilt up)
            imu_lock = 500;
            if (menu_index > 0) menu_index--;
            else menu_index = 3;
            screen_show();
            music_flag = 3;
        } 
        else if (key_up & 0x02 || ((imudata.gy < -80.00) && (imu_lock == 0))) 
        {  
            // Move selection down (button2 or tilt down)
            imu_lock = 500;
            if (menu_index < 3) menu_index++;
            else menu_index = 0;
            music_flag = 3;
            screen_show();
        }
        else if (key_up & 0x04)
        {
            // Confirm selection (button3)
            if (menu_index == 0) screen_state = 1;
            else if (menu_index == 1) screen_state = 2;
            else if (menu_index == 2) screen_state = 3;
            else if (menu_index == 3) screen_state = 4;
            music_flag = 3;
        }
    }
    else if (screen_state == 1) // Game screen
    {
        if (key_up & 0x04) 
        {
            // Trigger weapon action (button3)
            weapon_state = 1;
        }
        if (key_up & 0x08)
        {
            // Return to main menu (button4)
            screen_state = 0;
        }
    }
    else if (screen_state == 2) // Settings menu
    {
        if (key_up & 0x08)
        {
            // Save settings and return
            screen_state = 0;
            setting_change();
        }
        if (key_up & 0x02)
        {
            // Move selection down
            if (setting_index < 2)
                setting_index++;
        }
        if (key_up & 0x01)
        {
            // Move selection up
            if (setting_index > 0)
                setting_index--;
        }
    }
    else if (screen_state == 3) // Help menu
    {
        if (key_up & 0x08)
        {
            // Return to main menu
            screen_state = 0;
        }
        if (key_up & 0x02 || ((imudata.gy < -80.00) && (imu_lock == 0)))
        {
            // Next help page (button2 or tilt down)
            if (instruction_index < 2) {
                imu_lock = 500;
                instruction_index++;
            }
        }
        if (key_up & 0x01 || ((imudata.gy > 80.00) && (imu_lock == 0)))
        {
            // Previous help page (button1 or tilt up)
            if (instruction_index > 0) {
                imu_lock = 500;
                instruction_index--;
            }
        }
    }
    else if (screen_state == 4) // Achievements screen
    {
        if (key_up & 0x08)
        {
            // Return to main menu
            screen_state = 0;
        }
        if (key_up & 0x02 || ((imudata.gy < -80.00) && (imu_lock == 0)))
        {
            // Next achievement page
            if (achievement_index < 2) {
                achievement_index++;
                imu_lock = 500;
            }
        }
        if (key_up & 0x01 || ((imudata.gy > 80.00) && (imu_lock == 0)))
        {
            // Previous achievement page
            if (achievement_index > 0)
            {
                achievement_index--;
                imu_lock = 500;
            }
        }
    }
    else if (screen_state == 5) // Game over screen
    {
        if (key_up & 0x08)
        {
            // Return to main menu
            screen_state = 0;
            game_init();
        }
        if (key_up & 0x04)
        {
            // Restart game
            screen_state = 1;
            game_init();
        }
    }
    else if (screen_state == 6) // Pause screen
    {
        if (key_up & 0x08)
        {
            // Return to main menu
            screen_state = 0;
            game_init();
        }
        if (key_up & 0x04)
        {
            // Resume game
            screen_state = 1;
            game_init();
        }
    }
}

// Function to scan button states and return as 4-bit value
uint8_t key_scan()
{
    uint8_t temp = 0;
    if (button1.read() == 1) temp |= 1;  // bit0 -> button1
    if (button2.read() == 1) temp |= 2;  // bit1 -> button2
    if (button3.read() == 1) temp |= 4;  // bit2 -> button3
    if (button4.read() == 1) temp |= 8;  // bit3 -> button4
    return temp;
}
